package org.budo.support.spring.web.util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.budo.support.lang.util.ListUtil;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author limingwei
 */
public class RequestMappingUtil {
    public static List<String> getRequestMapping(Method method) {
        RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
        if (null == methodAnnotation) {
            return null;
        }

        String[] methodMappings = methodAnnotation.value();
        RequestMapping classAnnotation = method.getDeclaringClass().getAnnotation(RequestMapping.class);
        if (null == classAnnotation) {
            return ListUtil.toList(methodMappings);
        }

        String[] classMappings = classAnnotation.value();
        if (null == classMappings || classMappings.length < 1) {
            return ListUtil.toList(methodMappings);
        }

        // 方法上未指定 url ，用 类上的
        if (null == methodMappings) {
            return ListUtil.toList(classMappings);
        }

        List<String> mappings = new ArrayList<String>();
        // 方法和类上都有
        for (String classMapping : classMappings) {
            for (String methodMapping : methodMappings) {
                mappings.add(classMapping + methodMapping);
            }
        }

        return mappings;
    }
}